package com.shiwenping.reply.data.remote;

import android.content.Context;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;

import com.shiwenping.reply.data.DataSource;
import com.shiwenping.reply.data.RealmUtil;
import com.shiwenping.reply.net.ApiManager;
import com.shiwenping.reply.net.body.FaqListBeen;
import com.shiwenping.reply.net.domain.FaqItemResult;
import com.shiwenping.reply.net.domain.FaqListResult;
import com.shiwenping.reply.net.domain.FileItemResult;
import com.shiwenping.reply.net.domain.FileListResult;
import com.shiwenping.reply.net.service.AllService;
import com.shiwenping.reply.tools.AllUtils;

import java.util.ArrayList;
import java.util.List;

import io.realm.Realm;
import io.realm.RealmList;
import io.realm.RealmResults;
import okhttp3.ResponseBody;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 线上数据库
 *
 * @author shiwenping
 * @date 17-11-10 上午10:04
 */

public class RemoteDataSource implements DataSource {
    private Realm realm;
    private Context mContext;

    private Realm getRealm() {
        if (realm == null) {
            realm = RealmUtil.getRealm();
        }
        return realm;
    }

    public RemoteDataSource(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public Observable<List<FaqItemResult>> getFaqList(boolean isLocal,String cateName) {
        return ApiManager.retrofit(mContext)
                .create(AllService.class)
                .getFaqList(new FaqListBeen(0, 100).build())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .flatMap(faqListResult -> {
                    if (faqListResult.isSuccess()) {
                        return Observable.from(AllUtils.toObs(faqListResult.getData()));
                    }
                    return null;
                })
                .flatMap(faqItemResults -> {
//                    Log.d("HaiChecker", "是否为主线程:"+String.valueOf(Looper.getMainLooper() == Looper.myLooper()));
                    getRealm().beginTransaction();
                    getRealm().delete(FaqItemResult.class);
                    getRealm().commitTransaction();
                    for (FaqItemResult faqItemResult : faqItemResults) {
                        getRealm().beginTransaction();
                        getRealm().insertOrUpdate(faqItemResult);
                        getRealm().commitTransaction();
                    }

                    if (TextUtils.isEmpty(cateName))
                    {
                        return Observable.from(AllUtils.toObs(faqItemResults));
                    }else {
                        return Observable.from(AllUtils.toObs(faqItemResults.where()
                                .like("faq_tag","*"+cateName+"*").findAll()));
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
//                .flatMap(faqItemResults -> getRealm().where(FaqItemResult.class).findAllAsync().asObservable()
//                .filter(RealmResults::isLoaded))
                .flatMap(faqItemResults -> {
                    Log.d("HaiChecker","知识库大小:"+String.valueOf(faqItemResults.size()));
                    List<FaqItemResult> faqItemResults1 = new ArrayList<>();
                    faqItemResults1.addAll(faqItemResults);
                    return Observable.from(AllUtils.toObs(faqItemResults1));
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io());
    }

    @Override
    public Observable<List<FileItemResult>> getFileList(boolean isLocal,String cateName) {
        return ApiManager.retrofit(mContext)
                .create(AllService.class)
                .getFileList(new FaqListBeen(0, 100).build())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .flatMap(fileListResult -> {
                    if (fileListResult.isSuccess()) {
                        return Observable.from(AllUtils.toObs(fileListResult.getData()));
                    }
                    return null;
                })
                .flatMap(faqItemResults -> {
                    for (FileItemResult faqItemResult : faqItemResults) {
                        getRealm().beginTransaction();
                        getRealm().insertOrUpdate(faqItemResult);
                        getRealm().commitTransaction();
                    }
                    if (TextUtils.isEmpty(cateName))
                    {
                        return Observable.from(AllUtils.toObs(faqItemResults));
                    }else {
                        return Observable.from(AllUtils.toObs(faqItemResults.where()
                                .like("file_tag","*"+cateName+"*").findAll()));
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .flatMap(faqItemResults -> {
                    Log.d("HaiChecker","文件数量:"+String.valueOf(faqItemResults.size()));
                    List<FileItemResult> faqItemResults1 = new ArrayList<>();
                    faqItemResults1.addAll(faqItemResults);
                    return Observable.from(AllUtils.toObs(faqItemResults1));
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io());
    }
}
